package com.example.androidhw03;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ListView;

public class MainActivity extends Activity {

	ListView listView;
	EditText msgView;
	CheckBox check;
	MyAdapter mAdapter;
	ArrayList<MyData> mList = new ArrayList<MyData>();
	boolean checked = false;
	Bitmap bm;
	
	public static final int REQUEST_GET_IMAGE = 0;
	public static final int REQUEST_CROP_IMAGE = 1;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		msgView = (EditText)findViewById(R.id.msgEdit);
		check = (CheckBox)findViewById(R.id.sendCheck);
		listView = (ListView)findViewById(R.id.dataView);
				
		mAdapter = new MyAdapter(MainActivity.this, mList);
		
		listView.setAdapter(mAdapter);
		
		Button send = (Button)findViewById(R.id.sendBtn);
		send.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				MyData data;
				checked = check.isChecked();
				String msg = msgView.getText().toString();
								
				if(msg!=null && !msg.equals("")){
					if(checked == true){
						data = new MyData("YOU", msg, checked, false, false);
					}else{
						data = new MyData(msg, checked, false, false);
					}
					mAdapter.add(data);
				}
				msgView.setText("");
			}
		});
		
		Button dateBtn = (Button)findViewById(R.id.dateBtn);
		dateBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				mAdapter.addDate(new MyData(true, false));
			}
		});
		
		Button pictureBtn = (Button)findViewById(R.id.pictureBtn);
		pictureBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent i = new Intent(Intent.ACTION_GET_CONTENT);
				i.setType("image/*");
				startActivityForResult(i, REQUEST_GET_IMAGE);
//				mAdapter.addPicture(new MyData(true, bm));
			}
		});
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		if(requestCode == REQUEST_GET_IMAGE){
			if(resultCode == RESULT_OK){
				Uri uri = data.getData();
				
				Intent intent = new Intent("com.android.camera.action.CROP");
				intent.setDataAndType(uri, "image/*");
				intent.putExtra("outputX", 90);
				intent.putExtra("outputY", 90);
				intent.putExtra("aspectX", 1);
				intent.putExtra("aspectY", 1);
				intent.putExtra("return-data", true);
				startActivityForResult(intent, REQUEST_CROP_IMAGE);
			}
		}else if(requestCode == REQUEST_CROP_IMAGE){
			if(resultCode == RESULT_OK){
				checked = check.isChecked();
				bm = data.getParcelableExtra("data");	
				mAdapter.addPicture(new MyData(checked, false, true, bm));
			}
		}
		super.onActivityResult(requestCode, resultCode, data);
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

}
